/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.ef;

import wtkx.Component;
import wtkx.Effect;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

/**
 * Base class shape type rect paints to backing image.
 *
 * @author jdp
 */
public class Image
    extends Rect
{
    protected BufferedImage backing;

    private float inX, inY, inW, inH;


    public Image(){
        super();
    }


    protected final void checkIn(){
        this.inW = this.width;
        this.inH = this.height;
    }
    protected final boolean hasComponentResized(){
        return (this.inW != this.width || this.inH != this.height);
    }
    public void layout(Component component){
        this.checkIn();
        super.layout(component);
        BufferedImage backing = this.backing;
        if (null == backing || this.hasComponentResized()){
            if (null != backing)
                backing.flush();

            int w = (int)this.width;
            int h = (int)this.height;
            if (0 < w && 0 < h){
                this.backing = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);//_PRE
            }
        }
    }
    protected final Graphics2D createGraphics(){
        BufferedImage backing = this.backing;
        if (null != backing){
            return backing.createGraphics();
        }
        else
            throw new IllegalStateException();
    }
    protected final Graphics2D createGraphics(int x, int y, int w, int h){
        BufferedImage backing = this.backing;
        if (null != backing){
            Graphics2D g = backing.createGraphics();
            g.clipRect(x,y,w,h);
            return g;
        }
        else
            throw new IllegalStateException();
    }

    public void destroy(){
        BufferedImage backing = this.backing;
        if (null != backing){
            this.backing = null;
            backing.flush();
        }
    }
    public Graphics2D prepare(Component component, Graphics2D g){
        return g;
    }
    public void update(Graphics2D g){
    }

}
